﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraNavBar;
using System.Windows.Forms;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTab;
using DevExpress.XtraTreeList;
using MM.Common.Base;
using DevExpress.XtraTreeList.Nodes;
using MM.Common.DBBase;
using System.Drawing;
using DevExpress.XtraNavBar.ViewInfo;
using DevExpress.XtraEditors.Controls;
using System.Drawing.Drawing2D;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraPrinting;
using MM.Commmon.UI;

namespace MM.Common.Support
{
    public partial class FrmFormSetup : FrmFunctionWithTool
    {
        public FrmFormSetup()
        {
            InitializeComponent();
        }

        private void ChoiceEmp()
        {
            if (this.dtEmpAll == null)
            {
                this.dtEmpAll = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT EMP_ID ,EMP_CODE ,EMP_NAME FROM MXH_EMPLOYEE ORDER BY EMP_CODE", ref this.dtEmpAll);
                this.dtEmpAll.PrimaryKey = new DataColumn[] { this.dtEmpAll.Columns["EMP_ID"] };
            }
            string str = this.tvFunction.FocusedNode.GetValue("CODE").ToString();
            FrmFunctionPointEmp emp = new FrmFunctionPointEmp();
            emp.SetParaMeter(this.dtEmpAll, this.dtEmp, str);
            if (emp.ShowDialog(this) == DialogResult.OK)
            {
                this.xtraTabControl1.SelectedTabPage = this.xtraTabPage2;
            }
            emp.Dispose();
        }

        private void ChoiceRole()
        {
            if (this.dtRoleAll == null)
            {
                this.dtRoleAll = new DataTable();
                SystemInfo.DbData.Retrieve("SELECT ROLE_CODE ,ROLE_NAME FROM MXHLY_SYS_ROLE ORDER BY ROLE_CODE", ref this.dtRoleAll);
                this.dtRoleAll.PrimaryKey = new DataColumn[] { this.dtRoleAll.Columns["ROLE_CODE"] };
            }
            string str = this.tvFunction.FocusedNode.GetValue("CODE").ToString();
            FrmFunctionPointRole role = new FrmFunctionPointRole();
            role.SetParaMeter(this.dtRoleAll, this.dtRole, str);
            if (role.ShowDialog(this) == DialogResult.OK)
            {
                this.xtraTabControl1.SelectedTabPage = this.xtraTabPage1;
            }
            role.Dispose();
        }

        private void DelNode()
        {
            TreeListNode focusedNode = this.tvFunction.FocusedNode;
            if (focusedNode.ImageIndex != 0)
            {
                if (focusedNode.Nodes.Count > 0)
                {
                    XtraMessageBox.Show("请先删除功能子节点! ", "提示");
                }
                else if (XtraMessageBox.Show("是否真的删除" + focusedNode.GetValue("CODE").ToString() + "?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (focusedNode.ImageIndex == 2)
                    {
                        string paramValue = focusedNode.GetValue("CODE").ToString();
                        string commandtext = "DELETE FROM KTSYS_FORM WHERE FORM_CODE = :form_code";
                        if (SystemInfo.DbData.ExecuteSql(commandtext, new DataParamCol(new DataParam[] { new InputDataParam("form_code", paramValue) })) >= 0)
                        {
                            this.tvFunction.Nodes.Remove(focusedNode);
                        }
                        else
                        {
                            XtraMessageBox.Show("删除窗口不成功!\n" + SystemInfo.DbData.ErrorMessage);
                        }
                    }
                    else
                    {
                        string str3 = focusedNode.GetValue("CODE").ToString();
                        string[] commandtexts = new string[3];
                        DataParamCol[] parms = new DataParamCol[3];
                        commandtexts[0] = "DELETE FROM MXHLY_SYS_FUNCTION_POINT WHERE FUNCTION_POINT_CODE = :fcode ";
                        parms[0] = new DataParamCol(new DataParam[] { new InputDataParam("fcode", str3) });
                        commandtexts[1] = "DELETE FROM MXHLY_SYS_FUNCTION_POINT_ROLE WHERE FUNCTION_POINT_CODE = :fcode ";
                        parms[1] = new DataParamCol(new DataParam[] { new InputDataParam("fcode", str3) });
                        commandtexts[2] = "DELETE FROM MXHLY_SYS_FUNCTION_POINT_EMP WHERE FUNCTION_POINT_CODE = :fcode ";
                        parms[2] = new DataParamCol(new DataParam[] { new InputDataParam("fcode", str3) });
                        if (SystemInfo.DbData.RunSql(commandtexts, parms) == 0)
                        {
                            this.tvFunction.Nodes.Remove(focusedNode);
                        }
                        else
                        {
                            XtraMessageBox.Show("删除功能点不成功!\n" + SystemInfo.DbData.ErrorMessage);
                        }
                    }
                }
            }
        }


        private void EditNode()
        {
            TreeListNode focusedNode = this.tvFunction.FocusedNode;
            if (focusedNode.ImageIndex > 1)
            {
                switch (focusedNode.ImageIndex)
                {
                    case 2:
                        {
                            FrmFormEdit edit = new FrmFormEdit();
                            edit.SetParameter(focusedNode);
                            edit.ShowDialog(this);
                            return;
                        }
                    case 3:
                        {
                            FrmFunctionPointEdit edit2 = new FrmFunctionPointEdit();
                            edit2.SetParameter(focusedNode, false);
                            edit2.ShowDialog(this);
                            return;
                        }
                }
            }
        }

        private void FreshNode(int index)
        {
            this.nbiNew.Visible = index >= 1;
            this.nbiEdit.Visible = index >= 2;
            this.nbiDelete.Visible = index >= 2;
            this.nbiRole.Visible = index == 3;
            this.nbiEmp.Visible = index == 3;
            this.splitContainerControl1.Panel2.Visible = index == 3;
        }

        private void FreshRoleAndEmp(string function_point_code)
        {
            string commandtext = "SELECT F.ROLE_CODE, R.ROLE_NAME ,F.PROPERTY \r\n   FROM MXHLY_SYS_FUNCTION_POINT_ROLE F, MXHLY_SYS_ROLE R \r\n   WHERE F.ROLE_CODE = R.ROLE_CODE AND F.FUNCTION_POINT_CODE = :pcode \r\n   ORDER BY F.ROLE_CODE ASC";
            DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("pcode", function_point_code) });
            this.dtRole.Rows.Clear();
            SystemInfo.DbData.Retrieve(commandtext, parm, ref this.dtRole);
            this.dtRole.PrimaryKey = new DataColumn[] { this.dtRole.Columns["ROLE_CODE"] };
            this.gvRole.GridControl.DataSource = this.dtRole;
            string str2 = "SELECT F.EMP_ID, E.EMP_CODE ,E.EMP_NAME ,F.PROPERTY \r\n   FROM MXHLY_SYS_FUNCTION_POINT_EMP F, MXH_EMPLOYEE E \r\n   WHERE F.EMP_ID = E.EMP_ID AND F.FUNCTION_POINT_CODE = :pcode \r\n   ORDER BY E.EMP_CODE ASC";
            this.dtEmp.Rows.Clear();
            SystemInfo.DbData.Retrieve(str2, parm, ref this.dtEmp);
            this.dtEmp.PrimaryKey = new DataColumn[] { this.dtEmp.Columns["EMP_ID"] };
            this.gvEmp.GridControl.DataSource = this.dtEmp;
        }

        private void gvEmp_DoubleClick(object sender, EventArgs e)
        {
            this.ChoiceEmp();
        }

        private void gvRole_DoubleClick(object sender, EventArgs e)
        {
            this.ChoiceRole();
        }

        public override bool Init()
        {
            bool flag = false;
            if (base.Init())
            {
                try
                {
                    DataTable table = new DataTable();
                    table.Columns.Add("PROPERTY_CODE", typeof(string));
                    table.Columns.Add("PROPERTY_NAME", typeof(string));
                    table.Rows.Add(new object[] { "H", "不可视" });
                    table.Rows.Add(new object[] { "R", "只读" });
                    table.Rows.Add(new object[] { "W", "全部" });
                    DataTable table2 = table.Copy();
                    table.Rows.Add(new object[] { " ", "    " });
                    this.riProperty.ValueMember = "PROPERTY_CODE";
                    this.riProperty.DisplayMember = "PROPERTY_NAME";
                    this.riProperty.DataSource = table;
                    this.riPropertyRole.ValueMember = "PROPERTY_CODE";
                    this.riPropertyRole.DisplayMember = "PROPERTY_NAME";
                    this.riPropertyRole.DataSource = table2;
                    this.riPropertyEmp.ValueMember = "PROPERTY_CODE";
                    this.riPropertyEmp.DisplayMember = "PROPERTY_NAME";
                    this.riPropertyEmp.DataSource = table2;
                    this.dtRole = new DataTable();
                    this.gvRole.GridControl.DataSource = this.dtRole;
                    this.dtEmp = new DataTable();
                    this.gvEmp.GridControl.DataSource = this.dtEmp;
                    DataTable dt = new DataTable();
                    string commandtext = "SELECT MODULE_CODE ,MODULE_NAME ,MODULE_FILENAME ,' ' FROM MXHLY_SYS_MODULE ORDER BY MODULE_CODE";
                    SystemInfo.DbData.Retrieve(commandtext, ref dt);
                    string str2 = "SELECT FORM_CODE ,FORM_NAME ,OBJECT_NAME ,' ' \r\n    FROM KTSYS_FORM \r\n    WHERE MODULE_CODE = :module_code \r\n    ORDER BY FORM_CODE ASC ";
                    string str3 = "SELECT FUNCTION_POINT_CODE ,FUNCTION_POINT_NAME ,'' ,DEFAULT_PROPERTY\r\n    FROM MXHLY_SYS_FUNCTION_POINT \r\n   WHERE FORM_CODE = :form_code \r\n   ORDER BY FUNCTION_POINT_CODE ";
                    this.tvFunction.BeginUpdate();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string paramValue = dt.Rows[i]["MODULE_CODE"].ToString();
                        TreeListNode parentNode = this.tvFunction.AppendNode(dt.Rows[i], this.tvFunction.Nodes[0]);
                        parentNode.SelectImageIndex = 1;
                        parentNode.ImageIndex = 1;
                        DataTable table4 = new DataTable();
                        DataParamCol parm = new DataParamCol(new DataParam[] { new InputDataParam("module_code", paramValue) });
                        SystemInfo.DbData.Retrieve(str2, parm, ref table4);
                        for (int j = 0; j < table4.Rows.Count; j++)
                        {
                            string str5 = table4.Rows[j]["FORM_CODE"].ToString();
                            TreeListNode node2 = this.tvFunction.AppendNode(table4.Rows[j], parentNode);
                            node2.SelectImageIndex = 2;
                            node2.ImageIndex = 2;
                            DataTable table5 = new DataTable();
                            parm = new DataParamCol(new DataParam[] { new InputDataParam("form_code", str5) });
                            SystemInfo.DbData.Retrieve(str3, parm, ref table5);
                            for (int k = 0; k < table5.Rows.Count; k++)
                            {
                                TreeListNode node3 = this.tvFunction.AppendNode(table5.Rows[k], node2);
                                node3.SelectImageIndex = 3;
                                node3.ImageIndex = 3;
                            }
                        }
                    }
                    Cursor.Current = Cursors.Default;
                    this.tvFunction.EndUpdate();
                    this.tvFunction.Nodes[0].Expanded = true;
                    base.ZoomWin();
                    this.FreshNode(0);
                    flag = true;
                }
                catch (Exception exception)
                {
                    XtraMessageBox.Show("初始化对象不成功!\n" + exception.Message, "出错");
                }
            }
            return flag;
        }

        private void nbiDelete_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.DelNode();
        }

        private void nbiEdit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.EditNode();
        }

        private void nbiEmp_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.ChoiceEmp();
        }

        private void nbiExit_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiNew_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.NewNode();
        }

        private void nbiPrint_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;
            //if (PrintHelper.IsPrintingAvailable)
            //{
            //    PrintHelper.ShowPreview(this.tvFunction);
            //}
            //else
            //{
            //    XtraMessageBox.Show("打印库文件未找到！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //}
            Cursor.Current = Cursors.Default;
        }

        private void nbiRole_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            this.ChoiceRole();
        }

        private void NewNode()
        {
            TreeListNode focusedNode = this.tvFunction.FocusedNode;
            if (focusedNode.ImageIndex != 0)
            {
                if (focusedNode.ImageIndex == 3)
                {
                    focusedNode = focusedNode.ParentNode;
                }
                switch (focusedNode.ImageIndex)
                {
                    case 1:
                        {
                            FrmFormAdd add = new FrmFormAdd();
                            add.SetParameter(focusedNode);
                            add.ShowDialog(this);
                            return;
                        }
                    case 2:
                        {
                            FrmFunctionPointEdit edit = new FrmFunctionPointEdit();
                            edit.SetParameter(focusedNode, true);
                            edit.ShowDialog(this);
                            return;
                        }
                }
            }
        }

        private void tvFunction_DoubleClick(object sender, EventArgs e)
        {
            this.EditNode();
        }

        private void tvFunction_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {
            if (e.Node.ImageIndex == 0)
            {
                this.FreshNode(0);
            }
            else
            {
                if (e.Node.ImageIndex == 3)
                {
                    this.FreshRoleAndEmp(e.Node.GetValue(this.tlcCode).ToString());
                }
                this.FreshNode(e.Node.ImageIndex);
            }
        }
    }
}
